﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.Framework;
using System.IO;
using CNVP.JShop.UI;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Admin
{
    public partial class LinkEdit : AdminPage
    {
        BLL.CNVP_Links bll = new BLL.CNVP_Links();
        Model.CNVP_Links model = new Model.CNVP_Links();
        public int id = 0;
        public string picOriginal = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 122)) return;//判断页面权限
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]) && Public.IsNumber(Request.QueryString["ID"]))
            {
                id = Convert.ToInt32(Request.QueryString["ID"]);
            }
            if (!IsPostBack)
            {
                ddlLinkClass();
                if (id > 0)
                {
                    lbTitle.Text = " > 修改";
                    DataInfo(id);
                }
                else
                {
                    int LinkClassID = 0;
                    if (!string.IsNullOrEmpty(Request.QueryString["LinkClassID"]) && Public.IsNumber(Request.QueryString["LinkClassID"]))
                    {
                        LinkClassID = Convert.ToInt32(Request.QueryString["LinkClassID"]);
                    }
                    if (LinkClassID > 0)
                    {
                        ddlLinkClassID.SelectedValue = LinkClassID.ToString();
                    }
                    lbTitle.Text = " > 添加";
                }
            }
            
        }
        private void ddlLinkClass()
        {
            IList<Model.CNVP_Column> model = new BLL.CNVP_Column().GetAll_CNVP_Column("ColumnType = 1 and IsShow=1 order by OrderID");
            foreach (Model.CNVP_Column c in model)
            {
                ddlLinkClassID.Items.Add(new ListItem(c.ColumnName, c.ID.ToString()));
            }
        }
        public void DataInfo(int id)
        {
            model = bll.GetSingle_CNVP_LinksById(id);
            if (model != null)
            {

                txtLinkName.Text = model.LinkName;
                txtLinkAddress.Text = model.LinkAddress;
                if (model.LinkPic != "")
                {
                    imgPic.Visible = true;
                    string[] AdPicArray = model.LinkPic.Split(Convert.ToChar("|"));
                    if (AdPicArray.Length > 1)
                    {
                        imgPic.ImageUrl = Public.GetFirst(model.LinkPic);
                        picOriginal = Public.GetSecond(model.LinkPic);
                    }
                    else
                    {
                        imgPic.ImageUrl = model.LinkPic;
                        picOriginal = model.LinkPic;
                    }
                }
                txtPicOldName.Text = model.LinkPic;
                rblLinkType.SelectedValue = model.LinkType.ToString();
                txtOrderID.Text = model.OrderID.ToString();
                ddlLinkClassID.SelectedValue = model.LinkClassID.ToString();
            }
        }
        
        protected void btUp_Click(object sender, EventArgs e)
        {
            if (txtLinkName.Text.Trim() == "")
            {
                BasePage.MessageBoxGoHistory("友链名称不能为空！");
                return;
            }
            string newName = "";
            string newName_Small = "";
            string PicName = "";
            string root = Server.MapPath("/UploadFile/Product/");
            string strRoot = "/UploadFile/Product/";
            if (id > 0)
            {
                if (this.fileUp.PostedFile.ContentLength != 0)
                {
                    string fileName = fileUp.PostedFile.FileName;
                    string postfix = fileName.Substring(fileName.LastIndexOf("."));
                    if (postfix.ToLower() != ".jpg" && postfix.ToLower() != ".bmp" && postfix.ToLower() != ".gif" && postfix.ToLower() != ".jpeg" && postfix.ToLower() != ".png")
                    {
                        BasePage.MessageBox("上传的图片必须为.jpg或.bmp或.gif或.jpeg或.png！");
                        return;
                    }
                    newName = Public.getRandomFilename(root) + "Link" + postfix;
                    fileUp.PostedFile.SaveAs(root + "\\" + newName);
                    newName_Small = Public.parseFn(newName, "_400px300px");
                    Public.MakeThumbnail(root + "\\" + newName, root + "\\" + newName_Small, 400, 300, "W");
                    PicName = strRoot + newName_Small + "|" + strRoot+newName;
                }
                else
                {
                    PicName = txtPicOldName.Text;
                }
                int rows = new BLL.CNVP_Links().Update_CNVP_Links(
                       new Model.CNVP_Links()
                       {
                           ID = id,
                           LinkName = txtLinkName.Text.Trim(),
                           LinkAddress = txtLinkAddress.Text.Trim(),
                           LinkPic = PicName,
                           LinkType = int.Parse(rblLinkType.SelectedValue),
                           OrderID = int.Parse(txtOrderID.Text),
                           LinkClassID = int.Parse(ddlLinkClassID.SelectedValue)
                       }
               );
                if (rows == 1)
                {
                    if (this.fileUp.PostedFile.ContentLength != 0)
                    {
                        BasePage.DeleteFile(root + "\\" + Public.GetFirst(txtPicOldName.Text.Trim()));
                        BasePage.DeleteFile(root + "\\" + Public.GetSecond(txtPicOldName.Text.Trim()));
                    }
                    txtPicOldName.Text = PicName;
                    BasePage.MessageBox("修改成功！", "LinkList.aspx?Page=" + Request.QueryString["Page"] + "");
                }
                else
                { BasePage.MessageBoxGoHistory("修改失败！"); }
            }
            else
            {
                if (this.fileUp.PostedFile.ContentLength != 0)
                {
                    string fileName = fileUp.PostedFile.FileName;
                    string postfix = fileName.Substring(fileName.LastIndexOf("."));
                    if (postfix.ToLower() != ".jpg" && postfix.ToLower() != ".bmp" && postfix.ToLower() != ".gif" && postfix.ToLower() != ".jpeg" && postfix.ToLower() != ".png")
                    {
                        BasePage.MessageBox("上传的图片必须为.jpg或.bmp或.gif或.jpeg或.png！");
                        return;
                    }
                    newName = Public.getRandomFilename(root) + "Link" + postfix;
                    fileUp.PostedFile.SaveAs(root + "\\" + newName);
                    newName_Small = Public.parseFn(newName, "_400px300px");
                    Public.MakeThumbnail(root + "\\" + newName, root + "\\" + newName_Small, 400, 300, "W");
                    PicName = strRoot + newName_Small + "|" + strRoot + newName;
                }
                else
                {
                    PicName = txtPicOldName.Text.Trim();
                }
                int rows = new BLL.CNVP_Links().Add_CNVP_Links(
                    new Model.CNVP_Links()
                    {
                        LinkName = txtLinkName.Text.Trim(),
                        LinkAddress = txtLinkAddress.Text.Trim(),
                        LinkPic = PicName,
                        LinkType = int.Parse(rblLinkType.SelectedValue),
                        OrderID = int.Parse(txtOrderID.Text),
                        LinkClassID = int.Parse(ddlLinkClassID.SelectedValue)
                    }
                );
                if (rows == 1)
                {
                    BasePage.MessageBox("添加成功！", "LinkList.aspx");
                }
                else
                { BasePage.MessageBoxGoHistory("添加失败！"); }

            }
        }
    }
}